//
//  NetworkState.swift
//
//  Created by ios on 2022/5/30.
//
import Foundation
import Alamofire
import UIKit

public class XGL_NetworkStatusManager {
    
    public var networkStatus: NetworkReachabilityManager.NetworkReachabilityStatus = .unknown
    private var manager: NetworkReachabilityManager?
    
    public static let shared: XGL_NetworkStatusManager = {
        let shared = XGL_NetworkStatusManager()
        shared.manager = NetworkReachabilityManager(host: "www.baidu.com")
        return shared
    }()
    private init() {}
    
    public var isReachable: Bool{
        return self.manager!.isReachable
    }
    
    public func start() {
        manager?.startListening(onUpdatePerforming: {[weak self] (status) in
            guard let `self` = self else { return }
            self.networkStatus = status
            self.checkNetworkStatus()
        })
    }
    
    func checkNetworkStatus() {
//        guard let window = UIApplication.shared.currentWindow else { return  }
        switch networkStatus {
        case .notReachable:
//            let alertCtrl = UIAlertController(title: "Tips", message: "Please check your network connection ", preferredStyle: .alert)
//            let cancelAction = UIAlertAction(title: "Sure", style: .cancel, handler: nil)
//            alertCtrl.addAction(cancelAction)
//            window.visibleViewController().present(alertCtrl, animated: true, completion: nil)
            break
        case .unknown:
//            let alertCtrl = UIAlertController(title: "Tips", message: "Please check your network connection ", preferredStyle: .alert)
//            let cancelAction = UIAlertAction(title: "Sure", style: .cancel, handler: nil)
//            alertCtrl.addAction(cancelAction)
//            window.visibleViewController().present(alertCtrl, animated: true, completion: nil)
            break
        case .reachable(.ethernetOrWiFi):
            debugPrint("network avalible")
            XGL_EncipherTool.share.getPublicKey()
            break
        case .reachable(.cellular):
            XGL_EncipherTool.share.getPublicKey()
            break
        }
        
    }
}

